//
//  ZZHomeTopMenu.m
//  QQplayground
//
//  Created by zhangxy on 2018/6/6.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "ZZHomeTopMenu.h"

@interface ZZHomeTopMenu(){
    UIColor *selectColor;
    UIColor *textColor;
    
    UIButton *checkBtn;
    
    UIImageView *imgLine;
}

@property(nonatomic,strong)UIScrollView *scroll;
@property(nonatomic,strong) void (^pComplateBlock)(NSInteger tag,NSString *title);

@end

@implementation ZZHomeTopMenu

-(id)initWithArray:(NSArray *)array startImage:(NSString *)imageName result:(void(^)(NSInteger tag,NSString *title)) complateBlock{
    self = [super initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
    if(self){
        _pComplateBlock = complateBlock;
        _scroll = [[UIScrollView alloc] initWithFrame:self.bounds];
        [_scroll setBackgroundColor:UIColor.clearColor];
        _scroll.alwaysBounceHorizontal = YES;
        _scroll.alwaysBounceVertical = NO;
        _scroll.pagingEnabled = NO;
        //    _mainScroll.bounces = NO;
        _scroll.scrollEnabled = YES;
        
        [self addSubview:_scroll];
        [self addItemView:array imageName:imageName];
    }
    return self;
}

-(id)initWithFrame:(CGRect)f array:(NSArray *)array textColor:(UIColor *)color checkColor:(UIColor *)selColor result:(void (^)(NSInteger, NSString *))complateBlock{
   
    self = [super initWithFrame:f];
    if(self){
        textColor = color;
        selectColor = selColor;
        _pComplateBlock = complateBlock;
        _scroll = [[UIScrollView alloc] initWithFrame:self.bounds];
        [_scroll setBackgroundColor:UIColor.clearColor];
        _scroll.alwaysBounceHorizontal = YES;
        _scroll.alwaysBounceVertical = NO;
        _scroll.pagingEnabled = NO;
        //    _mainScroll.bounces = NO;
        _scroll.scrollEnabled = YES;
        
        [self addSubview:_scroll];
        [self addItemView:array imageName:nil];
    }
    return self;
}

-(void)addItemView:(NSArray *) arr imageName:(NSString *) name{
    int i = 0;
    CGFloat cw = 54.0f;
    CGFloat mw = self.frame.size.width;
    int count = (int)arr.count;
    if(convertToString(name).length > 0){
        count = count + 1;
    }
    
    if(mw / count > 54){
        cw = mw / count;
    }
//    
//    if(arr.count < 4 && convertToString(name).length == 0){
//        cw = ScreenWidth / 4;
//    }
    CGFloat x = 0;
    if(convertToString(name).length > 0){
        UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
        b.titleLabel.font = FontFiftent;
        if(textColor!=nil){
            [b setTitleColor:textColor forState:0];
        }else{
            [b setTitleColor:UIColorFromRGB(TextWhiteColor) forState:0];
        }
        if(selectColor!=nil){
            
            [b setTitleColor:selectColor forState:UIControlStateSelected];
        }else{
            [b setTitleColor:UIColorFromRGB(TextWhiteColor) forState:0];
        }
        [b setImage:[UIImage imageNamed:name] forState:0];
        b.tag = i;
        [b setFrame:CGRectMake(0, 0, cw, 44)];
        [b addTarget:self action:@selector(onItemClick:) forControlEvents:UIControlEventTouchUpInside];
        [_scroll addSubview:b];
        
        x = 54;
    }
    i = i+1;
    for (NSString *item in arr) {
        UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
        b.titleLabel.font = FontFiftent;
        if(textColor!=nil){
            [b setTitleColor:textColor forState:0];
        }else{
            [b setTitleColor:UIColorFromRGB(TextWhiteColor) forState:0];
        }
        if(selectColor!=nil){
            
            [b setTitleColor:selectColor forState:UIControlStateSelected];
        }else{
            [b setTitleColor:UIColorFromRGB(TextWhiteColor) forState:0];
        }
        [b setTitle:item forState:0];
        [b setFrame:CGRectMake(x + (i-1)*cw, 2, cw, 30)];
        b.tag = i;
        [b addTarget:self action:@selector(onItemClick:) forControlEvents:UIControlEventTouchUpInside];
        
        if(i == 1){
            checkBtn = b;
            b.selected = YES;
        }
        
        [_scroll addSubview:b];
        i = i+1;
        
    }
    
    imgLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(checkBtn.frame)+2, 20, 2)];
    if(selectColor!=nil){
        [imgLine setBackgroundColor:selectColor];
    }else{
        [imgLine setBackgroundColor:UIColorFromRGB(BgTitleColor)];
    }
    // smallScrollView高度44，取下面4个点的高度为下划线的高度。
    imgLine.layer.cornerRadius = 1.0f;
    imgLine.layer.masksToBounds = YES;
    CGPoint c = imgLine.center;
    c.x = checkBtn.center.x;
    imgLine.center = c;
    [_scroll addSubview:imgLine];
    
    [_scroll setContentSize:CGSizeMake(cw * i, 44)];
}

-(void)onItemClick:(UIButton *) btn{
    DLog(@"点击------");
    
    if(checkBtn!=nil){
        checkBtn.selected = NO;
    }
    btn.selected = YES;
    checkBtn = btn;
    CGPoint c = imgLine.center;
    c.x = checkBtn.center.x;
    imgLine.center = c;
    if(_pComplateBlock){
        _pComplateBlock(btn.tag,btn.titleLabel.text);
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
